/** <!--------------------------------------------------------------------------------------------->
 * @file RevGraph.cpp
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */

// -- includes --
#include "RevGraph.h"
#include "RevGraphItem.h"
#include "RevGraphItemMap.h"
#include "RevGraphBranch.h"

#include <QAction>
#include <QDebug>


/** <!--------------------------------------------------------------------------------------------->
 * Creates the RevGraph object
 <!---------------------------------------------------------------------------------------------> */
RevGraph::RevGraph()
{
	setScene( new QGraphicsScene(this) );
	setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

}	// END RevGraph()

/** <!--------------------------------------------------------------------------------------------->
 * Deletes the RevGraph object
 <!---------------------------------------------------------------------------------------------> */
RevGraph::~RevGraph()
{
}	// END ~RevGraph()

/** <!--------------------------------------------------------------------------------------------->
 * Visualizes the given node and sets the focus on it
 <!---------------------------------------------------------------------------------------------> */
void RevGraph::Visualize( const SvnRevisionIdentifier& rNodeID )
{
	printf( "SI_RevisionRequested\n" ); fflush( stdout );

	scene()->clear();
	m_iCenterRev = rNodeID.m_iRevNumber;

//	// Create item at the scene
//	RevGraphItem* pGraphItem = new RevGraphItem( &m_iCenterRev, rNodeID.m_iRevNumber );
//	scene()->addItem( pGraphItem );	// moves ownership to the scene!!
//	// Add item to the global map
//	RevGraphItemLink oLink = m_aItemMap.insert( pGraphItem );
//	pGraphItem->setGlobalLink( oLink );

	// I need this emit, because otherwise we cannot cross the thread boundery
	emit SI_RevisionRequested( rNodeID );

}	// END Visualize( const SvnRevisionIdentifier& )

/** <!----------------------------------------------------------------------------------------->
 * Updates the given revision model item (or visualizes it at the screen, if it was not created
 * before).
 <!-----------------------------------------------------------------------------------------> */
void RevGraph::SL_UpdateRevision( QSP<RevModelItem> pRevModelItem )
{
	RevGraphBranch* pBranch = m_aBranches.get( pRevModelItem->m_iBranchIndex );
	if( !pBranch )
	{
		qWarning() << "Cannot find branch for item: " << pRevModelItem->m_iBranchIndex;
		return;
	}

	// Check whether this revision is already visible
	if( m_aItemMap.contains(pRevModelItem->m_iRevision) )
	{
		RevGraphItem* pGraphItem = m_aItemMap.get( pRevModelItem->m_iRevision );
		pGraphItem->setData( pRevModelItem );
		if( !pBranch->containsItem(pGraphItem) )
		{
			pBranch->addItem(pGraphItem);
		}
	}
	else
	{
		// Create item at the scene
		RevGraphItem* pGraphItem = new RevGraphItem( &m_iCenterRev, pRevModelItem );
		scene()->addItem( pGraphItem );	// moves ownership to the scene!!
		pBranch->addItem(pGraphItem);
		// Add item to the global map
		RevGraphItemLink pPos = m_aItemMap.insert( pGraphItem );
		pGraphItem->setGlobalLink( pPos );
	}
}	// END SL_UpdateRevision( QSP<RevModelItem> )

/** <!--------------------------------------------------------------------------------------------->
 * Updates the given branch (branch index is used by RevItemModel as index)
 <!---------------------------------------------------------------------------------------------> */
void RevGraph::SL_UpdateBranch( BranchIndex iBranchIndex, QString strBranchName )
{
	if( !m_aBranches.contains(iBranchIndex) )
	{
		RevGraphBranch* pBranch = new RevGraphBranch( strBranchName );
		pBranch->setBranchLink( m_aBranches.insert( iBranchIndex, pBranch ) );
		scene()->addItem( pBranch );	// moves ownership to the scene!!
	}
	else
	{
		qWarning() << "SL_UpdateBranch() of existing branch: " << iBranchIndex << strBranchName;
	}

}	// END SL_UpdateBranch( int, QString )

/** <!----------------------------------------------------------------------------------------->
 * Visualizes the status message on the screen
 <!-----------------------------------------------------------------------------------------> */
void RevGraph::SL_VisualizeStatus( const QString& strStatus )
{
	Q_UNUSED(strStatus);

}	// END SL_VisualizeStatus( const QString& )



